package com.game.worm.mobile.view.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import com.dx.as.common.FragmentUtils;
import com.dx.as.fragment.TFragment;
import com.game.worm.mobile.R;
import com.game.worm.mobile.view.common.CommonFragment;
import com.game.worm.mobile.view.common.OnFragmentHideListener;
import com.game.worm.mobile.view.list.typelist.HotFragment;
import com.game.worm.mobile.view.list.typelist.NewFragment;
import com.game.worm.mobile.view.list.typelist.SingeFragment;
import com.game.worm.mobile.view.list.typelist.WebFragment;

/**
 * Created by d_xxiang on 2018/3/27.
 */

public class ListFragment extends TFragment {


    private RadioButton rg_hot,rg_new,rg_danji,rg_web;
    private RadioButton [] radioButtons;

    @Override
    public void initView() {
        hideToolbar(false);
        rg_danji = findView(R.id.rb_danji);
        rg_hot = findView(R.id.rb_hot);
        rg_new = findView(R.id.rb_new);
        rg_web = findView(R.id.rb_web);

        rg_danji.setOnClickListener(onClickListener);
        rg_hot.setOnClickListener(onClickListener);
        rg_new.setOnClickListener(onClickListener);
        rg_web.setOnClickListener(onClickListener);

        radioButtons = new RadioButton[]{rg_hot,rg_new,rg_danji,rg_web};
    }

    @Override
    public int setLayoutI() {
        return R.layout.fragment_list;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            hideToolbar(false);
            switchFragment();
        }
    }


    @Override
    public void dataOpion() {
        HotFragment hotFragment = new HotFragment();
        FragmentUtils.putDefaultFragment(this,R.id.listContainer,hotFragment);
        ((CommonFragment)FragmentUtils.getFragmentByClassName(HotFragment.class)).setOnFragmentHideListener(onFragmentHideListener);
        _index = 0;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int index = Integer.parseInt(v.getTag().toString());
            for (RadioButton radioButton : radioButtons){
                radioButton.setChecked(false);
            }
            _index = index;
            radioButtons[index].setChecked(true);
            switchFragment();
        }
    };

    private void switchFragment(){
        switch (_index){
            case 0:
                FragmentUtils.goToChildFragment(ListFragment.this,HotFragment.class,R.id.listContainer);
                break;
            case 1:
                FragmentUtils.goToChildFragment(ListFragment.this,NewFragment.class,R.id.listContainer);
                ((CommonFragment)FragmentUtils.getFragmentByClassName(NewFragment.class)).setOnFragmentHideListener(onFragmentHideListener);
                break;
            case 2:
                FragmentUtils.goToChildFragment(ListFragment.this,SingeFragment.class,R.id.listContainer);
                ((CommonFragment)FragmentUtils.getFragmentByClassName(SingeFragment.class)).setOnFragmentHideListener(onFragmentHideListener);
                break;
            case 3:
                FragmentUtils.goToChildFragment(ListFragment.this,WebFragment.class,R.id.listContainer);
                ((CommonFragment)FragmentUtils.getFragmentByClassName(WebFragment.class)).setOnFragmentHideListener(onFragmentHideListener);
                break;
        }
    }


    private int _index = -1;

    @Override
    public void initRpo() {

    }

    private OnFragmentHideListener onFragmentHideListener = new OnFragmentHideListener() {
        @Override
        public void onChildHiddenChanged(boolean hide) {
            if (hide)
                onHiddenChanged(hide);
        }
    };


}
